package de.unibi.mmi.homeauto.team1.devices;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import de.unibi.mmi.homeauto.team1.R;
import de.unibi.mmi.homeauto.team1.client.Client;
import de.unibi.mmi.homeauto.team1.constants.BATH;
import de.unibi.mmi.homeauto.team1.constants.BEDROOM;
import de.unibi.mmi.homeauto.team1.constants.CORRIDOR;
import de.unibi.mmi.homeauto.team1.constants.DeviceValues;
import de.unibi.mmi.homeauto.team1.constants.KITCHEN;
import de.unibi.mmi.homeauto.team1.constants.LIVINGROOM;
import de.unibi.mmi.homeauto.team1.drafts.DraftFragment;
import de.unibi.mmi.homeauto.team1.mainview.MainViewActivity;
import de.unibi.mmi.homeauto.team1.rooms.RoomViewPager;

public class LampFragment extends DraftFragment {

	@Override
	protected int getLayoutID() {
		return R.layout.lampfragment;
	}

	Button livingRoomLamp1;
	Button livingRoomLamp2;

	Button bedRoomLamp1;
	Button bedRoomLamp2;

	Button bathLamp1;
	Button bathLamp2;

	Button kitchenLamp1;
	Button kitchenLamp2;

	Button corridorLamp;

	@Override
	public void loadComponents(View view) {
		

		((ImageView) view.findViewById(R.id.lamp_bathroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.BATH_POS);
					}
				});

		((ImageView) view.findViewById(R.id.lamp_livingroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.LIVING_ROOM_POS);
					}
				});

		((ImageView) view.findViewById(R.id.lamp_bedroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.BEDROOM_POS);
					}
				});

		((ImageView) view.findViewById(R.id.lamp_kitchen_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.KITCHEN_POS);
					}
				});

		((ImageView) view.findViewById(R.id.lamp_corridor_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.CORRIDOR_POS);
					}
				});

		corridorLamp = (Button) view.findViewById(R.id.lamp_corridor_button);
		corridorLamp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						CORRIDOR.LAMP_ID,
						Client.getValue(CORRIDOR.LAMP_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		bedRoomLamp1 = (Button) view.findViewById(R.id.lamp_bedroom_button1);
		bedRoomLamp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						BEDROOM.LAMP_1_ID,
						Client.getValue(BEDROOM.LAMP_1_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		bedRoomLamp2 = (Button) view.findViewById(R.id.lamp_bedroom_button2);
		bedRoomLamp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						BEDROOM.LAMP_2_ID,
						Client.getValue(BEDROOM.LAMP_2_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		kitchenLamp1 = (Button) view.findViewById(R.id.lamp_kitchen_button1);
		kitchenLamp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						KITCHEN.LAMP_1_ID,
						Client.getValue(KITCHEN.LAMP_1_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		kitchenLamp2 = (Button) view.findViewById(R.id.lamp_kitchen_button2);
		kitchenLamp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						KITCHEN.LAMP_2_ID,
						Client.getValue(KITCHEN.LAMP_2_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		livingRoomLamp1 = (Button) view
				.findViewById(R.id.lamp_livingroom_button1);
		livingRoomLamp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						LIVINGROOM.LAMP_1_ID,
						Client.getValue(LIVINGROOM.LAMP_1_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		livingRoomLamp2 = (Button) view
				.findViewById(R.id.lamp_livingroom_button2);
		livingRoomLamp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						LIVINGROOM.LAMP_2_ID,
						Client.getValue(LIVINGROOM.LAMP_2_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		bathLamp1 = (Button) view.findViewById(R.id.lamp_bathroom_button1);
		bathLamp1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						BATH.LAMP_1_ID,
						Client.getValue(BATH.LAMP_1_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		bathLamp2 = (Button) view.findViewById(R.id.lamp_bathroom_button2);
		bathLamp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mva.setServerValueAndUpdate(
						BATH.LAMP_2_ID,
						Client.getValue(BATH.LAMP_2_ID) == DeviceValues.LAMP_OFF ? DeviceValues.LAMP_ON
								: DeviceValues.LAMP_OFF);
			}
		});

		((Button) view.findViewById(R.id.lamp_all_button_on))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mva.setServerValue(BATH.LAMP_1_ID, DeviceValues.LAMP_ON);
						mva.setServerValue(BATH.LAMP_2_ID, DeviceValues.LAMP_ON);
						mva.setServerValue(LIVINGROOM.LAMP_1_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValue(LIVINGROOM.LAMP_2_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValue(BEDROOM.LAMP_1_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValue(BEDROOM.LAMP_2_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValue(KITCHEN.LAMP_1_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValue(KITCHEN.LAMP_2_ID,
								DeviceValues.LAMP_ON);
						mva.setServerValueAndUpdate(CORRIDOR.LAMP_ID,
								DeviceValues.LAMP_ON);
					}
				});

		((Button) view.findViewById(R.id.lamp_all_button_off))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						mva.setServerValue(BATH.LAMP_1_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(BATH.LAMP_2_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(LIVINGROOM.LAMP_1_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(LIVINGROOM.LAMP_2_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(BEDROOM.LAMP_1_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(BEDROOM.LAMP_2_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(KITCHEN.LAMP_1_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValue(KITCHEN.LAMP_2_ID,
								DeviceValues.LAMP_OFF);
						mva.setServerValueAndUpdate(CORRIDOR.LAMP_ID,
								DeviceValues.LAMP_OFF);
					}
				});
	}

	@Override
	public void serverValuesUpdated() {
		if (Client.getValue(CORRIDOR.LAMP_ID) == DeviceValues.LAMP_ON) {
			corridorLamp.setEnabled(true);
			corridorLamp.setText("An");
		} else if (Client.getValue(CORRIDOR.LAMP_ID) == DeviceValues.LAMP_OFF) {
			corridorLamp.setEnabled(true);
			corridorLamp.setText("Aus");
		} else {
			corridorLamp.setEnabled(false);
			corridorLamp.setText("Defekt");
		}

		if (Client.getValue(BATH.LAMP_1_ID) == DeviceValues.LAMP_ON) {
			bathLamp1.setEnabled(true);
			bathLamp1.setText("An");
		} else if (Client.getValue(BATH.LAMP_1_ID) == DeviceValues.LAMP_OFF) {
			bathLamp1.setEnabled(true);
			bathLamp1.setText("Aus");
		} else {
			bathLamp1.setEnabled(false);
			bathLamp1.setText("Defekt");
		}

		if (Client.getValue(BATH.LAMP_2_ID) == DeviceValues.LAMP_ON) {
			bathLamp2.setEnabled(true);
			bathLamp2.setText("An");
		} else if (Client.getValue(BATH.LAMP_2_ID) == DeviceValues.LAMP_OFF) {
			bathLamp2.setEnabled(true);
			bathLamp2.setText("Aus");
		} else {
			bathLamp2.setEnabled(false);
			bathLamp2.setText("Defekt");
		}

		if (Client.getValue(LIVINGROOM.LAMP_1_ID) == DeviceValues.LAMP_ON) {
			livingRoomLamp1.setEnabled(true);
			livingRoomLamp1.setText("An");
		} else if (Client.getValue(LIVINGROOM.LAMP_1_ID) == DeviceValues.LAMP_OFF) {
			livingRoomLamp1.setEnabled(true);
			livingRoomLamp1.setText("Aus");
		} else {
			livingRoomLamp1.setEnabled(false);
			livingRoomLamp1.setText("Defekt");
		}

		if (Client.getValue(LIVINGROOM.LAMP_2_ID) == DeviceValues.LAMP_ON) {
			livingRoomLamp2.setEnabled(true);
			livingRoomLamp2.setText("An");
		} else if (Client.getValue(LIVINGROOM.LAMP_2_ID) == DeviceValues.LAMP_OFF) {
			livingRoomLamp2.setEnabled(true);
			livingRoomLamp2.setText("Aus");
		} else {
			livingRoomLamp2.setEnabled(false);
			livingRoomLamp2.setText("Defekt");
		}

		if (Client.getValue(BEDROOM.LAMP_1_ID) == DeviceValues.LAMP_ON) {
			bedRoomLamp1.setEnabled(true);
			bedRoomLamp1.setText("An");
		} else if (Client.getValue(BEDROOM.LAMP_1_ID) == DeviceValues.LAMP_OFF) {
			bedRoomLamp1.setEnabled(true);
			bedRoomLamp1.setText("Aus");
		} else {
			bedRoomLamp1.setEnabled(false);
			bedRoomLamp1.setText("Defekt");
		}

		if (Client.getValue(BEDROOM.LAMP_2_ID) == DeviceValues.LAMP_ON) {
			bedRoomLamp2.setEnabled(true);
			bedRoomLamp2.setText("An");
		} else if (Client.getValue(BEDROOM.LAMP_2_ID) == DeviceValues.LAMP_OFF) {
			bedRoomLamp2.setEnabled(true);
			bedRoomLamp2.setText("Aus");
		} else {
			bedRoomLamp2.setEnabled(false);
			bedRoomLamp2.setText("Defekt");
		}

		if (Client.getValue(KITCHEN.LAMP_1_ID) == DeviceValues.LAMP_ON) {
			kitchenLamp1.setEnabled(true);
			kitchenLamp1.setText("An");
		} else if (Client.getValue(KITCHEN.LAMP_1_ID) == DeviceValues.LAMP_OFF) {
			kitchenLamp1.setEnabled(true);
			kitchenLamp1.setText("Aus");
		} else {
			kitchenLamp1.setEnabled(false);
			kitchenLamp1.setText("Defekt");
		}

		if (Client.getValue(KITCHEN.LAMP_2_ID) == DeviceValues.LAMP_ON) {
			kitchenLamp2.setEnabled(true);
			kitchenLamp2.setText("An");
		} else if (Client.getValue(KITCHEN.LAMP_2_ID) == DeviceValues.LAMP_OFF) {
			kitchenLamp2.setEnabled(true);
			kitchenLamp2.setText("Aus");
		} else {
			kitchenLamp2.setEnabled(false);
			kitchenLamp2.setText("Defekt");
		}
	}

	@Override
	public String getTitle() {
		return "Lampen";
	}
}